package my.convert.attribute.processor.impl;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import my.convert.attribute.processor.AttributeProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MapTypeAttributeProcessor extends AttributeProcessor{
	public Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public Map<String, Class<?>> execute(Class<?> entityClazz, Map<String, Class<?>> mapper) {
		List<Field> fields = super.parseEntry(entityClazz);
		if (fields == null || fields.size() == 0 || mapper == null)
			return null;
		for (Field field : fields) {
			Class<?> fieldClazz = field.getType(); // 得到field的class及类型全路径
			if (fieldClazz.isAssignableFrom(Map.class)) { // 
				Type fc = field.getGenericType();
				if (fc == null)continue;

				if (fc instanceof ParameterizedType) // 【3】如果是泛型参数的类型
				{
					ParameterizedType pt = (ParameterizedType) fc;
					Type[] actualTypeArguments = pt.getActualTypeArguments();
					if(actualTypeArguments.length>0) {
						if(actualTypeArguments[1] instanceof Class) {
							Class<?> genericClazz = (Class<?>) actualTypeArguments[1]; // 【4】 得到泛型里的class类型对象。
							mapper.put(field.getName() + "[", genericClazz);
						}else {
							logger.debug("Map集合必须确定泛型，否则不能完成值的封装");
						}
					}else {
						logger.debug("Map集合必须确定泛型，否则不能完成值的封装");
					}
				}
			}
		}
		return mapper;
	}

}
